package com.ruicar.afs.cloud.afscase.mq.receiver.processor;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruicar.afs.cloud.afscase.approvetask.entity.CaseApproveRecord;
import com.ruicar.afs.cloud.afscase.approvetask.service.CaseApproveRecordService;
import com.ruicar.afs.cloud.afscase.common.step.StepParam;
import com.ruicar.afs.cloud.afscase.common.utils.StepUtil;
import com.ruicar.afs.cloud.afscase.infomanagement.entity.*;
import com.ruicar.afs.cloud.afscase.infomanagement.service.*;
import com.ruicar.afs.cloud.afscase.mq.approvesendinfo.service.ApproveInformInfoService;
import com.ruicar.afs.cloud.afscase.mq.approvesendinfo.vo.CaseLogInfo;
import com.ruicar.afs.cloud.afscase.mq.approvesendinfo.vo.UrgentInfo;
import com.ruicar.afs.cloud.common.core.enums.AfsEnumUtil;
import com.ruicar.afs.cloud.common.core.exception.AfsBaseException;
import com.ruicar.afs.cloud.common.modules.afscorebusiness.enums.*;
import com.ruicar.afs.cloud.common.modules.dto.mq.approve.*;
import com.ruicar.afs.cloud.common.modules.enums.ApplySceneEnum;
import com.ruicar.afs.cloud.common.modules.enums.OprTypeEnum;
import com.ruicar.afs.cloud.common.mq.rabbit.listener.AfsMqBizProcessor;
import com.ruicar.afs.cloud.common.mq.rabbit.message.MqTransCode;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.math.BigDecimal;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * <p>Description: 加急操作</p>
 *
 */
@AllArgsConstructor
@Slf4j
@Component
@ConditionalOnProperty(prefix = "com.ruicar.afs.cloud.mq.rabbit", name = "enable")
public class UrgentCaseProcessor implements AfsMqBizProcessor<UrgentInfoSubmitInfo> {

    private CaseBaseInfoService caseBaseInfoService;
    private ApproveInformInfoService approveInformInfoService;


    @Transactional(rollbackFor = Exception.class)
    public boolean processMessage(UrgentInfoSubmitInfo entity) {
        //加急处理
        if (!ObjectUtils.isEmpty(entity.getCaseUrgent())) {
            UrgentInfo urgentInfo = new UrgentInfo();
            try {
                /**加急*/
                this.changeCaseUrgent(entity);
                urgentInfo.setResult(WhetherEnum.YES.getCode());
                urgentInfo.setApplyNo(entity.getCaseUrgent().getApplyNo());
            }catch (Exception e){
                urgentInfo.setResult(WhetherEnum.NO.getCode());
                urgentInfo.setApplyNo(entity.getCaseUrgent().getApplyNo());
            }

            approveInformInfoService.turnUrgentResult(urgentInfo);

        }
        return true;
    }

    @Override
    public MqTransCode getCode() {
        return MqTransCode.AFS_POS_APPLY_CASE_CTM_TURN_URGENT;
    }

    public void changeCaseUrgent(UrgentInfoSubmitInfo infoSubmitInfo) throws Exception{
        /**
         * 加急操作
         */
            CaseUrgentDto caseUrgentDto=infoSubmitInfo.getCaseUrgent();
            CaseBaseInfo baseInfo=caseBaseInfoService.getOne(Wrappers.<CaseBaseInfo>query().lambda()
                    .eq(CaseBaseInfo::getApplyNo,caseUrgentDto.getApplyNo()));
            if(!ObjectUtils.isEmpty(baseInfo)){
                baseInfo.setIsUrgent(WhetherEnum.YES.getCode());
                baseInfo.setUrgentTime(new Date());
                baseInfo.setIsStick(WhetherEnum.YES.getCode());
                baseInfo.setStickTime(new Date());
                baseInfo.setPriority("7");
                caseBaseInfoService.updateById(baseInfo);
            }else{
                log.info("编号:"+caseUrgentDto.getApplyNo());
                throw new AfsBaseException("","数据不存在!");
            }
    }
}

